<template>
  <div class="page-content">
    <hy-table
      v-bind="contentTableConfig"
      :listData="listData"
      @selectionChange="selectionChange"
      :listCount="listCount"
      v-model:page="pageInfo"
    >
      <template #status="scope">
        <el-button
          size="mini"
          plain
          v-if="scope.row.enable === 0 || scope.row.enable === 1"
          :type="scope.row.enable ? 'success' : 'danger'"
          >{{ scope.row.enable ? '启用' : '禁用' }}</el-button
        >
        <el-button
          size="mini"
          plain
          v-if="scope.row.status === 0 || scope.row.status === 1"
          :type="scope.row.status ? 'success' : 'danger'"
          >{{ scope.row.status ? '上架' : '下架' }}</el-button
        >
      </template>
      <template #createAt="scope">
        {{ $filters.formatTime(scope.row.createAt) }}
      </template>
      <template #updateAt="scope">
        {{ $filters.formatTime(scope.row.updateAt) }}
      </template>
      <template #handler="scope">
        <el-button v-if="isDelete" type="danger" size="mini">删除</el-button>
        <el-button
          v-if="isUpdate"
          @click="handleEdit(scope)"
          type="primary"
          size="mini"
          >编辑</el-button
        >
      </template>

      <template #headerHandler>
        <el-button v-if="isCreate" type="primary">添加用户</el-button>
      </template>
      <template
        v-for="(item, idx) of otherPropsSlots"
        :key="idx"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </hy-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from '@/store'
import HyTable from '@/base-ui/table'
import { usePermission } from '@/hooks/use-permission'
export default defineComponent({
  name: 'PageContent',
  props: {
    pageName: {
      type: String,
      default: 'users'
    },
    contentTableConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    HyTable
  },
  setup(props) {
    const store = useStore()
    const pageInfo = ref({
      currentPage: 1,
      pageSize: 10
    })
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')
    watch(pageInfo, () => getPageData())
    const getPageData = (params: any = {}) => {
      if (!isQuery) {
        return
      }
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...params
        }
      })
    }
    getPageData()
    const listData = computed(() =>
      store.getters['system/getPageList'](props.pageName)
    )
    const listCount = computed(() =>
      store.getters['system/getPageCount'](props.pageName)
    )

    const selectionChange = (value: any) => {
      console.log(value)
    }
    const handleEdit = (scope: any) => {
      console.log(scope.row)
    }
    const otherSlots = ['status', 'createAt', 'updateAt', 'handler']
    const otherPropsSlots = props.contentTableConfig?.popList.filter(
      (r: any) => !otherSlots.includes(r.slotName)
    )
    return {
      listData,
      selectionChange,
      handleEdit,
      getPageData,
      listCount,
      pageInfo,
      otherPropsSlots,
      isCreate,
      isUpdate,
      isDelete
    }
  }
})
</script>

<style lang="less" scoped>
.page-content {
  padding: 20px;
  box-sizing: border-box;
}
</style>
